﻿using System;
using System.Linq;
using System.Transactions;

public partial class quangcao : System.Web.UI.Page
{
    chodientuDataContext db = new chodientuDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"] == null)
        {
            Response.Redirect("chuadangnhap.aspx");
        }   

        DropDownList1.DataSource = db.Tinhtrangs.Select(n=>n.TinhtrangID);
        DropDownList1.DataBind();

        var query = from n in db.Quangcaos
                    where n.Username == Session["user"].ToString()
                    select new {n.Maquangcao,n.Thongdiep,n.LoaiQC,n.Thoigianbd,n.Thoigiankt,n.TinhtrangID};
        GridView1.DataSource = query;
        GridView1.DataBind();
    }


    protected void submit_Click(object sender, EventArgs e)
    {
        Quangcao qc = new Quangcao();
        Quangcao temp = new Quangcao();

        var query = db.Quangcaos;
        int giatien = 0;
       // Response.Write("<script>alert('" + temp.Maquangcao + "')</script>");
        if (query.Count() != 0)
        {
            foreach (var q in query)
            {
                temp = q;
            }
            string s = temp.Maquangcao;
            int n = Int32.Parse(s) + 1;
            qc.Maquangcao = n.ToString();
        }
        else
        {
            qc.Maquangcao = "1";
        }

        qc.Username = Session["user"].ToString();
        qc.Logo_url = linksp.Text;
        qc.Thongdiep = thongdiep.Text;

        DateTime a = DateTime.Parse(ngaybatdau.Text);
        DateTime b = DateTime.Parse(denngay.Text);
        if (trangchu.Checked)
        {
            giatien = 1200000 * (b-a).Days;
            qc.LoaiquangcaoID = "QC1";
        }
        else {
            giatien = 900000 * (b - a).Days;
            qc.LoaiquangcaoID = "QC2"; 
        }

        qc.Thoigianbd = DateTime.Parse(ngaybatdau.Text);
        qc.Thoigiankt = DateTime.Parse(denngay.Text);
        qc.TinhtrangID = "Chưa chạy";

        using (TransactionScope ts = new TransactionScope())
        {
            try
            {
                var query1 = db.Thanhviens.Where(n => n.Username.Equals(Session["user"].ToString()));
                foreach (var i in query1)
                {
                    if (i.Diemtichluy >= giatien)
                    {
                        i.Diemtichluy -= giatien;
                    }
                    else { Response.Write("<script>alert('Điểm tích lũy không đủ');</script>"); return; }
                }
                db.Quangcaos.InsertOnSubmit(qc);
                db.SubmitChanges();
                ts.Complete();
                Response.Write("<script>alert('Đăng quảng cáothành công');</script>");
            }
            catch (Exception)
            {
                Response.Write("<script>alert('Đăng quảng cáo không thành công');</script>");
            }
        }
        Response.Redirect("quangcao.aspx");
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string tinhtrang = DropDownList1.SelectedValue.ToString();

        var query = from n in db.Quangcaos
                    where n.Username == Session["user"].ToString() && n.TinhtrangID == tinhtrang
                    select new { n.Maquangcao, n.Thongdiep, n.LoaiquangcaoID, n.Thoigianbd, n.Thoigiankt, n.Tinhtrang };

            GridView1.DataSource = query;
            GridView1.DataBind();
    }
}